package com.xudu.xianrendongculturaltravelbackend.service.impl;
import com.google.gson.Gson;
import com.xudu.xianrendongculturaltravelbackend.exception.ErrorCode;
import com.xudu.xianrendongculturaltravelbackend.exception.ThrowUtils;
import com.xudu.xianrendongculturaltravelbackend.model.dto.file.UploadPictureResult;
import com.xudu.xianrendongculturaltravelbackend.model.dto.scenic.ScenicUpdateRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xudu.xianrendongculturaltravelbackend.manager.FileManager;
import com.xudu.xianrendongculturaltravelbackend.model.dto.scenic.ScenicAddRequest;
import com.xudu.xianrendongculturaltravelbackend.model.entity.Scenic;
import com.xudu.xianrendongculturaltravelbackend.service.ScenicService;
import com.xudu.xianrendongculturaltravelbackend.mapper.ScenicMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author xudu
* @description 针对表【scenic】的数据库操作Service实现
* @createDate 2025-06-17 20:01:55
*/
@Service
public class ScenicServiceImpl extends ServiceImpl<ScenicMapper, Scenic>
    implements ScenicService{

    @Resource
    private FileManager fileManager;

    /**
     * 添加景点
     * @param scenicAddRequest 添加景点请求
     * @return 添加景点的id
     */
    @Override
    public Long addScenic(ScenicAddRequest scenicAddRequest) {
        Gson gson = new Gson();
        Scenic scenic = new Scenic();
        BeanUtils.copyProperties(scenicAddRequest, scenic);

        //处理标签
        List<String> scenicTags = scenicAddRequest.getTags();
        String scenicTagsJson = gson.toJson(scenicTags);
        scenic.setTags(scenicTagsJson);

        // 上传封面图片
        MultipartFile coverImage = scenicAddRequest.getCoverImage();
        // 设置路径前缀
        String uploadPathPrefix = scenic.getScenicName();
        UploadPictureResult coverImageResult = fileManager.uploadPicture(coverImage, uploadPathPrefix);
        scenic.setCoverImage(coverImageResult.getUrl());

        // 上传景点图片
        List<MultipartFile> scenicImages = scenicAddRequest.getScenicImages();
        List<String> scenicImageUrlList = scenicImages.stream().map(scenicImage -> {
            UploadPictureResult scenicImageResult = fileManager.uploadPicture(scenicImage, uploadPathPrefix);
            return scenicImageResult.getUrl();
        }).toList();

        scenic.setScenicImages(gson.toJson(scenicImageUrlList));

        boolean save = this.save(scenic);
        ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);

        //返回添加景点的id
        return scenic.getId();
    }

    /**
     * 更新景点信息
     * @param scenicUpdateRequest 更新景点信息请求
     * @return 是否更新成功
     */
    @Override
    public Boolean updateScenic(ScenicUpdateRequest scenicUpdateRequest) {
        Scenic scenic = new Scenic();
        BeanUtils.copyProperties(scenicUpdateRequest, scenic);

        Gson gson = new Gson();

        //处理标签
        List<String> scenicUpdateTags = scenicUpdateRequest.getTags();
        if (scenicUpdateTags != null){
            String scenicUpdateTagsJson = gson.toJson(scenicUpdateTags);
            scenic.setTags(scenicUpdateTagsJson);
        }

        // 更新封面图片
        MultipartFile scenicUpdateCoverImage = scenicUpdateRequest.getCoverImage();
        if (scenicUpdateCoverImage != null) {
            String uploadPathPrefix = scenic.getScenicName();
            UploadPictureResult coverImageResult = fileManager.uploadPicture(scenicUpdateCoverImage, uploadPathPrefix);
            scenic.setCoverImage(coverImageResult.getUrl());
        }

        // 更新景点图片
        List<String> keepScenicImages = scenicUpdateRequest.getKeepScenicImages();
        if (scenicUpdateRequest.getKeepScenicImages() == null || scenicUpdateRequest.getKeepScenicImages().isEmpty()) {
            scenic.setScenicImages("[]"); // 如果输入为空，则设置为空数组
            keepScenicImages = new java.util.ArrayList<>(); // 初始化为空列表
        } else {
            scenic.setScenicImages(gson.toJson(scenicUpdateRequest.getKeepScenicImages())); // 否则正常赋值
        }
        List<MultipartFile> scenicUpdateImages = scenicUpdateRequest.getScenicImages();
        if (scenicUpdateImages != null){
            String uploadPathPrefix = scenic.getScenicName();
            List<String> scenicUpdateImageUrlList = scenicUpdateImages.stream().map(scenicUpdateImage -> {
                UploadPictureResult scenicUpdateImageResult = fileManager.uploadPicture(scenicUpdateImage, uploadPathPrefix);
                return scenicUpdateImageResult.getUrl();
            }).toList();
            System.out.println("=================================================");
            System.out.println("scenicUpdateImageUrlList = " + scenicUpdateImageUrlList);
            keepScenicImages.addAll(scenicUpdateImageUrlList);
            System.out.println("=================================================");
            System.out.println("keepScenicImages = " + keepScenicImages);
            scenic.setScenicImages(gson.toJson(keepScenicImages));
        }

        // 更新
        boolean update = this.updateById(scenic);
        ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR);
        return true;

    }
}




